# ~*~ coding: utf-8 ~*~
import time

from apps.common.dbbase import BaseModel
from apps.common.dbtypes import JsonDictTextField
from apps.common.utils import generate_uuid
from apps.extensions import db

__all__ = ['InventoryGroup', 'InventoryGroupHost', 'InventoryHostVariable']


class InventoryGroup(BaseModel):
    """主机组"""
    __tablename__ = 'tasks_realtime_inventory_group'

    id = db.Column(db.String(36), default=generate_uuid, primary_key=True)
    name = db.Column(db.String(32), unique=True)                # 主机组名称
    vars = db.Column(JsonDictTextField)                         # 主机组变量
    memo = db.Column(db.TEXT)                                   # 主机组描述

    create_by = db.Column(db.VARCHAR(32), nullable=True)
    create_time = db.Column(db.INT, default=time.time)
    update_time = db.Column(db.INT, default=time.time, onupdate=time.time)

    def __str__(self):
        return '<InventoryGroup: %s>' % self.name


class InventoryGroupHost(BaseModel):
    """组内主机"""
    __tablename__ = 'tasks_realtime_inventory_host'

    id = db.Column(db.String(36), default=generate_uuid, primary_key=True)
    group_id = db.Column(db.String(36))
    asset_id = db.Column(db.String(36))

    create_by = db.Column(db.VARCHAR(32), nullable=True)
    create_time = db.Column(db.INT, default=time.time)
    update_time = db.Column(db.INT, default=time.time, onupdate=time.time)


class InventoryHostVariable(BaseModel):
    """主机变量"""
    __tablename__ = 'tasks_realtime_inventory_host_variable'

    id = db.Column(db.String(36), default=generate_uuid, primary_key=True)
    asset_id = db.Column(db.String(36))
    vars = db.Column(JsonDictTextField)

    create_by = db.Column(db.VARCHAR(32), nullable=True)
    create_time = db.Column(db.INT, default=time.time)
    update_time = db.Column(db.INT, default=time.time, onupdate=time.time)
